<?php
    require_once ("Includes/session.php");
    require_once ("Includes/simplecms-config.php"); 
    require_once ("Includes/connectDB.php");
    require_once("conf.php");   
    include("Includes/header.php"); 
    //check if admin
    confirm_is_admin();//this should be enough

    $val = trim($_SERVER['QUERY_STRING']);
    if(empty($val))
    {
    }
    else
    {
        $vals = explode('=',$val);
        $id = $vals[1];
        $query = "SELECT title FROM exams WHERE id = ? LIMIT 1";

        $check_name = $databaseConnection->prepare($query);
        $check_name->bind_param('s', $id);

        $check_name->execute();
        $check_name->store_result();

        if($check_name->error)
        {
            echo "<script type='text/javascript'>alert('Database query failed!')</script>";
        }
        elseif ($check_name->num_rows == 1)
        {
            $title;
            $check_name->bind_result($title);  
            $check_name->fetch();
            echo "<h2>".$title."</h2><br/>";       
            echo "<div id='add'>\n<form action=Admin_Exams.php method='post'><br/><br/><br/><br/>";
            echo"<fieldset>";
            echo "<input type=\"hidden\" name=\"exam_id\" value=\"$id\">";
            echo "<input type=\"submit\" name=\"Delete\" value=\"Delete Exam\" id=\"dlt\" />";
            echo "\n</fieldset>\n</form><br/>";
            echo"<p>\n<a href=\"index.php\">Cancel</a>\n</p>\n</div>";
            
            echo "<div id='edit'>\n<form action=Admin_Exams.php method='post'>";
            echo"<fieldset>";
            echo "<label for=\"examName\">New Title:</label> ";
            echo "<input type=\"hidden\" name=\"exam_id\" value=\"$id\">";
            echo "<input type=\"text\" name=\"examName\" value=\"\" id=\"examName\" required=\"\" /></br>";
            echo "<input type=\"submit\" name=\"ChangeN\" value=\"Change Name\" id=\"rst\" />\n</fieldset>\n</form></div>";

            echo '<div id="fullmain">';
            echo '<h2>Exam Questions</h2>';
            
            $dg = new C_DataGrid("SELECT * FROM questions","id", "questions");
            $dg -> set_query_filter("exam_id = ".$id);
            $dg -> set_dimension(800, 200); 
            $dg -> set_col_hidden("id"); 
            $dg -> set_col_hidden("exam_id"); 
            $dg -> set_col_title("AIndex", "CorrectChoice"); 
            
            $dg -> display();
            
            
            echo '</div>';
            echo '</div>';

            $check_name->close();
            include ("Includes/footer.php");
            exit;
            
        }

        else
        {
            echo "<script type='text/javascript'>alert('No exam with this id exists!')</script>";
        }
        
                
    }

    if(isset($_POST['ChangeN']))
    {
        if(isset($_POST['exam_id']) && isset($_POST['examName']))
        {

            $id = $_POST['exam_id'];
            $NTitle = trim($_POST['examName']);
            if(empty($NTitle))
            {
                echo "<script type='text/javascript'>alert('Title not valid')</script>";

            }
            else
            {
                $query1 = "UPDATE Exams SET title = ? WHERE id= ? LIMIT 1";

                $statement = $databaseConnection->prepare($query1);
                $statement->bind_param('ss' , $NTitle , $id);
                $statement->execute();
                $statement->store_result();
                $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
                echo($statement->affected_rows);
                echo($statement->num_rows);
                if ($creationWasSuccessful)
                {
                    echo "<script type='text/javascript'>alert('Update Successful')</script>";
                }
                else
                {
                    echo "<script type='text/javascript'>alert(\""."Insert Failed:  " . $statement ->errno ."\")</script>";
                    if($statement->errno == 1062 )//duplicate key
                        echo "<script type='text/javascript'>alert('An Exam with the same title exists!')</script>";
                    else
                        echo "<script type='text/javascript'>alert('An Error occured !')</script>";
                    //echo "<script type='text/javascript'>alert(\""."Insert Failed:  " . $statement ->errno ."\")</script>";//debug code
                }
            }
        }
        else
        {
            echo "<script type='text/javascript'>alert('Error!')</script>";
        }
        
    }

    if(isset($_POST['Delete']))
    {
        if(isset($_POST['exam_id']))
        {
            $id = $_POST['exam_id'];
            $query = "DELETE FROM exams WHERE id = ? LIMIT 1";
            $statement = $databaseConnection->prepare($query);
            $statement->bind_param('s', $id);
            $statement->execute();
            $statement->store_result();
            
            if ($statement->error)
            {
                echo "<script type='text/javascript'>alert('Delete Failed!')</script>";
            }

            elseif ($statement->affected_rows == 1)
            {
                echo "<script type='text/javascript'>alert('Delete action complete!')</script>";
            }
            else
            {
                echo "<script type='text/javascript'>alert('Delete action failed!!')</script>";
            }

            $statement->close();
        }
        else
        {
            echo "<script type='text/javascript'>alert('Error!')</script>";
        }
    }

    if(isset($_POST['Add']))
    {
        $EName = trim($_POST['examName']);
        if(empty($EName))
        {
            echo "<script type='text/javascript'>alert('Invalid Exam name')</script>";
        }
        else
        {
            $query1 = "INSERT INTO Exams (title) VALUES (?)";

            $statement = $databaseConnection->prepare($query1);
            $statement->bind_param('s', $EName);
            $statement->execute();
            $statement->store_result();
            $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
            if ($creationWasSuccessful)
            {
                echo "<script type='text/javascript'>alert('Creation Successful')</script>";
            }
            else
            {
                if($statement->errno == 1062 )//duplicate key
                    echo "<script type='text/javascript'>alert('An Exam with the same title exists!')</script>";
                else
                    echo "<script type='text/javascript'>alert('An Error occured !')</script>";
                //echo "<script type='text/javascript'>alert(\""."Insert Failed:  " . $statement ->errno ."\")</script>";//debug code
            }
       
        }
    }



?>

        <div id="add">
            <h2>Add Exam</h2>
                <form action="Admin_Exams.php" method="post">
                    <fieldset>
                    <legend>Add Exam</legend>
                    <ol>
                        <li>
                            <label for="examName">Name:</label> 
                            <input type="text" name="examName" value="" id="examName" required="" />
                        </li>
                    </ol>
                    <input type="submit" name="Add" value="Add" />
                    <p>
                        <a href="index.php">Cancel</a>
                    </p>
                </fieldset>
            </form>
        </div>

        <div id="edit">
            <h2>Edit Exam</h2>
            <p>Double click an exam to edit</p>    
            <?php
            $dg = new C_DataGrid("SELECT id,title FROM exams","id", "exams");
            $dg -> set_dimension(400, 300); 
            $dg -> set_col_hidden("id"); 
            $onSelectRow = <<<ONSELECTROW
            function(status, rowid)
            {
	            window.location = "Admin_Exams.php?id="+rowid;
            }
ONSELECTROW;


                //$dg->add_event("jqGridSelectRow", $onSelectRow);
                $dg -> add_event("jqGridDblClickRow", $onSelectRow);
                $dg -> display();
            ?>
        </div>

</div>

<?php include ("Includes/footer.php"); ?>